package nio.exercise;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Desc:
 * @author: cww
 * @DateTime: 2020-03-31 0:31
 */

public class FileReadByChannel {


    public static void main(String[] args) throws IOException {
        readContinue1();
    }

    public static void readContinue(){
        try {
            RandomAccessFile file = new RandomAccessFile(new File("G:\\split\\channel.txt"),"rw");
            FileChannel channel = file.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            while (true){
                int count = channel.read(buffer);
                if (count!=-1){
                    buffer.flip();
                    if (buffer.hasRemaining()){
                        System.out.println(buffer.get());
                    }
                    buffer.clear();
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readContinue1() throws IOException {
        FileReader reader = new FileReader("G:\\split\\channel.txt");
        while (true) {
            int i = reader.read();
            if (i!=-1){
                char[] buf = new char[1024];
                reader.read(buf);
                for (char c : buf) {
                    if (c!=0){
                        System.out.println(c);
                    }
                }

            }
        }

    }
}
